Sistema de Nombrado en Java (JNDI) [Parte I]

La forma m�s sencilla de b�squeda requiere que especifiquemos un conjunto de atributos que la entrada debe tener y el nombre del contexto fuente en el que realizar la b�squeda.

El siguiente c�digo crea un conjunto de atrivutos matchAttrs, que tiene dos atributos "telephonenumber" y "mail".

Especifica que las entradas cualificadas deben tener un atributo apellido ("sn") con un valor de "Geisel" y un atributo "mail" con cualquier valor. Luego llama a� DirContext.search() para buscar las entradas en el contexto "ou=People" que tienen los atributos especificados por matchAttrs.

// Specify the attributes to match
// Ask for objects that has a surname ("sn") attribute with 
// the value "Geisel" and the "mail" attribute
Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
matchAttrs.put(new BasicAttribute("sn", "Geisel"));
matchAttrs.put(new BasicAttribute("mail"));

// Search for objects that have those matching attributes
NamingEnumeration answer = ctx.search("ou=People", matchAttrs);

Podemos imprimir los resultados de esta forma.

while (answer.hasMore()) {
    SearchResult sr = (SearchResult)answer.next();
    System.out.println(">>>" + sr.getName());
    printAttrs(sr.getAttributes());
}

printAttrs() es similar al c�digo del ejemplo getAttributes()� que imprime un conjunto de atributos.

Ejecutar este ejemplo produce la siguiente salida.

# java SearchRetAll
>>>cn=Ted Geisel
attribute: sn
value: Geisel
attribute: objectclass
value: top
value: person
value: organizationalPerson
value: inetOrgPerson
attribute: jpegphoto
value: [B@1dacd78b
attribute: mail
value: [email protected]
attribute: facsimiletelephonenumber
value: +1 408 555 2329
attribute: cn
value: Ted Geisel
attribute: telephonenumber
value: +1 408 555 5252

.�Devolver los Atributos Seleccionados

El ejemplo anterior devuelve todos los atributos asociados con las entradas que satisfacen la consulta especificada. Podemos seleccionar los atributos devueltos pasando a search() un array de identificadores de atributos que queremos incluir en los resultados. Despu�s de crear un matchAttrs como se vi� anteriormente, tambi�n necesitamos crear el array de identificadores de atributos, como se muestra aqu�.

// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "telephonenumber", "golfhandicap", "mail"};

// Search for objects that have those matching attributes
NamingEnumeration answer = ctx.search("ou=People", matchAttrs, attrIDs);

Este ejemplo devuelve los atributos "sn", "telephonenumber", "golfhandicap", y "mail" de las entradas que tienen un atributo "mail" y tienen un atributo "sn" con el valor "Geisel".

Este ejemplo produce la siguiente salida. (La entrada no tiene un atributo "golfhandicap", por eso no se devuelve.)

# java Search 
>>>cn=Ted Geisel
attribute: sn
value: Geisel
attribute: mail
value: [email protected]
attribute: telephonenumber
value: +1 408 555 5252

COMPARTE ESTE ARTÍCULO

COMPARTIR EN FACEBOOK
COMPARTIR EN TWITTER
COMPARTIR EN LINKEDIN
COMPARTIR EN WHATSAPP
SIGUIENTE ARTÍCULO